package com.jeeplus.invoice.api;

import com.jeeplus.invoice.header.service.YxfpInvoiceHeaderService;
import com.jeeplus.invoice.header.service.dto.YxfpInvoiceHeaderDTO;
import com.jeeplus.invoice.header.service.mapstruct.YxfpInvoiceHeaderWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "越香发票-发票抬头API")
@RestController
@RequestMapping(value = "/invoice/api/header")
public class HeaderAPI {

    @Autowired
    private YxfpInvoiceHeaderService yxfpInvoiceHeaderService;

    @Autowired
    private YxfpInvoiceHeaderWrapper yxfpInvoiceHeaderWrapper;


    /**
     * 根据客户ID查询发票抬头列表数据
     */
    @ApiOperation(value = "根据客户ID查询发票抬头列表数据")
    @GetMapping("getInviceHeaderListByCustomerId")
    public ResponseEntity<List<YxfpInvoiceHeaderDTO>> getInviceHeaderListByCustomerId(String customerId) {
        return ResponseEntity.ok(yxfpInvoiceHeaderService.findListByCustomerId(customerId));
    }

    /**
     * 根据Id获取抬头管理数据
     */
    @ApiOperation(value = "根据Id获取抬头数据")
    @GetMapping("getInviceHeaderById")
    public ResponseEntity<YxfpInvoiceHeaderDTO> getInviceHeaderById(String id) {
        return ResponseEntity.ok (yxfpInvoiceHeaderService.findById (id));
    }

    /**
     * 保存抬头数据
     */
    @ApiOperation(value = "保存抬头数据")
    @PostMapping("saveInvoiceHeader")
    public ResponseEntity<String> saveInvoiceHeader(@Valid @RequestBody YxfpInvoiceHeaderDTO yxfpInvoiceHeaderDTO) {
        System.out.println("yxfpInvoiceHeaderDTO="+yxfpInvoiceHeaderDTO);
        if(yxfpInvoiceHeaderDTO.getDefaultFlag()!=null && yxfpInvoiceHeaderDTO.getDefaultFlag().equals("1")){
            yxfpInvoiceHeaderService.setNotDefalutInvoiceHeaderByCustomerID(yxfpInvoiceHeaderDTO.getCustomer().getId());
        }
        yxfpInvoiceHeaderService.saveOrUpdate(yxfpInvoiceHeaderWrapper.toEntity(yxfpInvoiceHeaderDTO));
        return ResponseEntity.ok("保存抬头管理成功");
    }

    /**
     * 设为默认抬头据
     */
    @ApiOperation(value = "设为默认抬头据")
    @PostMapping("setDefaultInvoiceHeader")
    public ResponseEntity<String> setDefaultInvoiceHeader(String id,String customerId) {
        yxfpInvoiceHeaderService.setNotDefalutInvoiceHeaderByCustomerID(customerId);
        yxfpInvoiceHeaderService.setDefaultInvoiceHeader(id);
        return ResponseEntity.ok("设为默认抬头据成功");
    }

    /**
     * 删除抬头数据
     */
    @ApiOperation(value = "删除抬头数据")
    @PostMapping("delInvoiceHeader")
    public ResponseEntity<String> delInvoiceHeader(String id) {
        yxfpInvoiceHeaderService.delInvoiceHeaderById(id);
        return ResponseEntity.ok("删除抬头数据");
    }

    /**
     * 根据客户Id获取默认抬头
     */
    @ApiOperation(value = "根据客户Id获取默认抬头")
    @GetMapping("getDefaultHeaderByCustomerId")
    public ResponseEntity<YxfpInvoiceHeaderDTO> getDefaultHeaderByCustomerId(String customerId) {
        return ResponseEntity.ok (yxfpInvoiceHeaderService.getDefaultHeaderByCustomerId (customerId));
    }

}
